<!--
 * @Description: 分类管理
 * @Author: 阳婷
 * @Date: 2021-03-18 10:34:01
 * @LastEditTime: 2022-03-29 14:20:33
 * @LastEditors: 阳婷
 :reserve-selection="true"表格加这个参数需要加row-key但是表格后会有空白
-->
<template>
    <div class='box-index'>
        <div class='filter-container'>
            <el-input class="input-width" size="small" placeholder="商品类型名称" v-model="keyword" clearable
                @change="getData(5)"></el-input>
            <el-button type="primary" class="filterBtn" size="small" @click="getData(6)">查询</el-button>
        </div>
        <div class="role-content">
            <table-header @click='Eventclick' :hideBtn="hideBtn">
                <el-button type="text" icon="fa fa-plus" @click='addData'> 添加</el-button>
            </table-header>
        </div>
        <el-table border style="width: 100%" :data="tableData" @sort-change='getsort' @row-click='rowclick'
            @selection-change="handleSelectionChange" ref="tables" highlight-current-row :row-key="getRowKeys">
            <el-table-column type="selection" :reserve-selection=true></el-table-column>
            <el-table-column prop="name" label="商品类型名称"></el-table-column>
            <!-- <el-table-column prop="remark" show-overflow-tooltip label="备注"></el-table-column> -->
        </el-table>
        <div class="pagination-container">
            <el-pagination background @current-change="handleCurrentChange" :page-size='perPage' :current-page="current"
                :total="pagetotal" layout="total, prev, pager, next, jumper">
            </el-pagination>
            <asides v-if="show" v-model:show="show" v-model:header="header" v-model:Icon='icon' v-model:width="width">
                <!-- <class-info :overlist="overlist" :row-data="rowData"></class-info> -->
                <side-info v-model:row-data="rowData"></side-info>
            </asides>
        </div>
    </div>
</template>
<script>
    import {
        ref,
        reactive,
        toRefs,
        defineComponent,
        onMounted,
        getCurrentInstance
    } from "vue";
    import TableHeader from "./../../components/TableHeader/index.vue"
    import Asides from "@/components/Asides"
    import sideInfo from './sideInfo.vue'
    export default defineComponent({
        setup(props) {
            const datas = reactive({
                hideBtn: {
                    Enable: false,
                    Disable: false,
                    delete: true
                },
                pagetotal: 2, //总条数
                page: 1, //当前页
                perPage: 15, //每页多少条
                current: 1,
                sort: '',
                keyword: '',
                tableData: [],
                chooseGroup: [],
                rowData: {},
                header: "",
                show: false,
                icon: "el-icon-menu",
                width: "560px"
            })
            const { proxy } = getCurrentInstance();
            const tables = ref(null);
            onMounted(() => {
                getData(1)
            });
            const getData = (val) => {
                datas.show?datas.show=false:""
                if (val == 1) {
                    datas.chooseGroup = []
                    proxy.$refs.tables.clearSelection();
                    datas.page = 1
                }
                proxy.$ajax('get', '/api/categories', {
                    page: datas.page,
                    perPage: datas.perPage,
                    keyword: datas.keyword,
                    sort: datas.sort
                }, (data) => {
                    datas.tableData = data.data.data
                    datas.current = data.data.pageNum ? data.data.pageNum : 1
                    datas.perPage = data.data.pageSize ? parseFloat(data.data.pageSize) : 15
                    datas.pagetotal = data.data.total
                })
            }
            const getRowKeys = (row) => {
                return row.id
            }
            const Eventclick = (val) => {
                if (val == "删除") {
                    deletes()
                }
            }
            const rowclick = (val) => {
                datas.show = true
                datas.header = val.name
                datas.rowData = val
            }
            const addData = () => {
                datas.show = true
                datas.rowData = {}
                datas.header = "添加"
            }
            const deletes = () => {
                proxy.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        proxy.$ajax(
                            "delete", "/api/categories", {
                            id: datas.chooseGroup
                        },
                            data => {
                                proxy.$message({
                                    type: "success",
                                    message: data.msg
                                });
                                proxy.$refs.tables.clearSelection();
                                datas.chooseGroup = []
                                proxy.getData(8)
                            }
                        );
                    })
                    .catch(() => {
                        proxy.$message({
                            type: "success",
                            message: "已取消删除"
                        })
                    })
            }
            const getsort = (val) => {
                if (val && val.order == 'ascending') {
                    datas.sort = val.prop
                } else if (val && val.order == 'descending') {
                    datas.sort = '-' + val.prop
                }
            }
            const handleSelectionChange = (val) => {
                datas.chooseGroup = []
                val.forEach(el => {
                    datas.chooseGroup.push(el.id)
                })
            }
            const handleCurrentChange = (val) => {
                datas.page = val
                getData(9)
            }
            return { ...toRefs(datas), tables, getData, Eventclick, rowclick, addData, getsort, getRowKeys, handleSelectionChange, handleCurrentChange }
        },
        components: {
            TableHeader,
            Asides,
            sideInfo
        }
    })

</script>